/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Task_info接口实现                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Repository.SqlServer                                  
*│　类    名： Task_infoRepository                                      
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.DbHelper;
using NetCoreCMS.IRepository;
using NetCoreCMS.Models;
using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Data;

namespace NetCoreCMS.Repository.SqlServer
{
    public class Task_infoRepository:BaseRepository<Task_info,Int32>, ITask_infoRepository
    {
        public Task_infoRepository(IOptionsSnapshot<DbOption> options)
        {
            _dbOption =options.Get("NetCoreCMS");
            if (_dbOption == null)
            {
                throw new ArgumentNullException(nameof(DbOption));
            }
            //_dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString);
        }
        
        public int DeleteLogical(int[] ids)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                string sql = "update [" + nameof(Task_info) + "] set IsDelete=1 where Id in @Ids";
                return _dbConnection.Execute(sql, new
                {
                    Ids = ids
                });
            }
        }

        public async Task<int> DeleteLogicalAsync(int[] ids)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                string sql = "update [" + nameof(Task_info) + "] set IsDelete=1 where Id in @Ids";
                return await _dbConnection.ExecuteAsync(sql, new
                {
                    Ids = ids
                });
            }
        }

        public async Task<bool> ResumeSystemStoppedAsync()
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                string sql = "update [" + nameof(Task_info) + "] set Status=0 where Status=3";
                return await _dbConnection.ExecuteAsync(sql) > 0;
            }
        }

        public async Task<bool> SystemStoppedAsync()
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                string sql = "update [" + nameof(Task_info) + "] set Status=3 where Status=0";
                return await _dbConnection.ExecuteAsync(sql) > 0;
            }
        }

        public async Task<bool> UpdateStatusByIdsAsync(int[] ids, int Status)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                string sql = "update [" + nameof(Task_info) + "] set Status=@Status where Id in @Ids";
                return await _dbConnection.ExecuteAsync(sql, new
                {
                    Status = Status,
                    Ids = ids,
                }) > 0;
            }
        }

        public async Task<List<Task_info>> GetListByJobStatuAsync(int Status)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                string sql = "select * from [" + nameof(Task_info) + "] where Status=@Status ";
                var result = await _dbConnection.QueryAsync<Task_info>(sql, new
                {
                    Status = Status,
                });
                if (result != null)
                {
                    return result.ToList();
                }
                else
                {
                    return new List<Task_info>();
                }
            }
        }


        public async Task<bool> IsExistsNameAsync(string Name)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                string sql = "select Id from [" + nameof(Task_info) + "] where Name=@Name";
                var result = await _dbConnection.QueryAsync<int>(sql, new
                {
                    Name = Name,
                });
                if (result != null && result.Count() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public async Task<bool> IsExistsNameAsync(string Name, int Id)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                string sql = "select Id from [" + nameof(Task_info) + "] where Name=@Name and Id <> @Id ";
                var result = await _dbConnection.QueryAsync<int>(sql, new
                {
                    Name = Name,
                    Id = Id,
                });
                if (result != null && result.Count() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public bool UpdateField(int id, string strValue)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                return _dbConnection.Execute("update [" + nameof(Task_info) + "] set " + strValue + " where id=@id", new { id = id }) > 0;
            }
        }
    }
}